package customer.zspt.cn.customer.components.group;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

import customer.zspt.cn.customer.Adapter.GroupViewAdapter;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.SkillInfoDao;
import customer.zspt.cn.customer.components.chat.FriendChat;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.GetAllInfoUtils;
import customer.zspt.cn.customer.utils.SPUtils;

/**
 * Created by Administrator on 2018/6/18 0018.
 */
public class group_view extends Activity {
    private TextView group_title;
    private TextView user_number;
    private String skillTitle;
    private GridView kfLs;
    private SkillInfoDao skillInfoDao;
    private Button send_msg;
    private String skillInfo;
    private String skillInfoStr;
    private LinearLayout user_ll;
    private Gson gson;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group_view);
        initUI();
        int skillId = getIntent().getIntExtra("skillId", -1);
        String token = SPUtils.getString(this, "token", "");
        skillInfoStr = SPUtils.getString(this,
                constant.SKILLGROUP_INFO + skillId,"");
        if(skillId==-1)return;
        getGroupData(skillId, token);

    }
    /**获取群成员列表*/
    private void getGroupData(int skillId, String token) {
        GetAllInfoUtils.infoUtils.getFriendInfo(constant.GROUPURL + skillId, token, this);
        GetAllInfoUtils.infoUtils.monitoringLinster(new GetAllInfoUtils.ReadyData() {
            @Override
            public void success(final String data) {
                System.out.println(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToPull(data);
                    }
                });
            }
            @Override
            public void fail() {

            }
        });
    }

    private void initUI() {
        group_title = findViewById(R.id.group_title);
        user_number = findViewById(R.id.user_number);
        user_ll = findViewById(R.id.user_ll);
        kfLs = (GridView) findViewById(R.id.kfLs);
        send_msg = (Button) findViewById(R.id.send_msg);
        send_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(group_view.this, FriendChat.class);
                intent.putExtra("skillInfoDao", skillInfoStr);
                intent.putExtra("type", "1");
                startActivity(intent);
            }
        });
    }
    public void ToPull(String data) {
        gson = new Gson();
        try {
            skillInfoDao = gson.fromJson(data, SkillInfoDao.class);
            skillTitle = skillInfoDao.data.skillTitle;
            int count = skillInfoDao.data.kefus.size();
            group_title.setText(skillTitle);
            user_number.setText(user_number.getText().toString() + count + "");
            GroupViewAdapter adapter = new GroupViewAdapter(this, skillInfoDao);
            kfLs.setAdapter(adapter);
            setOnClickLintener(skillInfoDao);
        } catch (Exception e) {
            Toast.makeText(this, "程序异常", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    private void setOnClickLintener(final SkillInfoDao skillInfoDao) {
        final List<SkillInfoDao.SkillInfo.Kefus> kefus = skillInfoDao.data.kefus;
        kfLs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplication(), FriendChat.class);
                SkillInfoDao.SkillInfo.Kefus UserInfo = kefus.get(position);
                String info = gson.toJson(UserInfo);
                intent.putExtra("type", "0");
                intent.putExtra("friendInfo", info);
                startActivity(intent);
            }
        });
    }


}
